var express = require('express');
var router = express.Router();
var sqlUtils = require('../sql/sqlUtil');
var multer = require('multer');
var Return = require('../util/utils');
var date = require("silly-datetime");
var fs = require("fs");

// 上传收款码
router.post("/image", multer({
    dest: "./public/upload/qr_code"
}).single("file"), (req, res) => {
    var file = req.file;
    var time = Date.now();
    try {
        var name=file.originalname.split('.')
        name = "."+name[name.length - 1];
        fs.renameSync(file.path, `./public/upload/qr_code/${time+name}`)
        file.path = `../upload/qr_code/${time+name}`;
        var json = Return.setResult();
        json.message = "上传成功";
        var data = {
            "resultdata": file.path,
            "type": 1
        };
        json.data = data;
        res.send(json);

    } catch (e) {
        console.log(e)
    }
})
// 上传付款凭证截图
router.post("/wximage", multer({
    dest: "./public/upload/screenshot/"
}).single("file"), (req, res) => {
    var file = req.file;
    try {
        fs.renameSync(file.path, `./public/upload/screenshot/${file.originalname}`)
        file.path = `../upload/screenshot/${file.originalname}`;
        var json = Return.setResult();
        json.message = "上传成功";
        var data = {
            "img_url": file.path
        };
        json.data = data;
        res.send(json);
    } catch (e) {
        console.log(e)
    }
})
// 上传新闻封面
router.post("/news", multer({
    dest: "./public/upload/news/"
}).single("file"), (req, res) => {
    var file = req.file;
    var time = Date.now();
    try {
        var name=file.originalname.split('.')
        name = "."+name[name.length - 1];
        fs.renameSync(file.path, `./public/upload/news/${time+name}`)
        file.path = `../upload/news/${time+name}`;
        res.send(Return.setResult(200,"success",{"url":file.path}));
    } catch (e) {
        console.log(e)
    }
})
// 上传新闻内容图片
router.post("/inner-images", multer({
    dest: "./public/upload/news/inner-images"
}).single("image"), (req, res) => {
    var url = "https://main.yefeiedu.cn/";
    var file = req.file;
    var time = Date.now();
    try {
        var name=file.originalname.split('.')
        name = "."+name[name.length - 1];
        fs.renameSync(file.path, `./public/upload/news/inner-images/${time+name}`)
        file.path = url + `upload/news/inner-images/${time+name}`;
        res.send(Return.setResult(200,"success",{"url":[file.path]}));
    } catch (e) {
        console.log(e)
    }
})
module.exports = router;